/**
 * 系统统计相关API
 */
import { apiClient, API_ENDPOINTS } from './index'
import type { ApiResponse } from './types/common'
import type {
  DashboardStats,
  MapPassword
} from './types/stats'

/**
 * 获取首页统计数据
 * @returns Promise
 */
export async function getDashboardStats(): Promise<ApiResponse<DashboardStats>> {
  try {
    // 直接调用getMostFavoriteItems获取数据
    return getMostFavoriteItems();
  } catch (error) {
    console.error('获取首页统计数据失败:', error);
    return {
      code: 1,
      message: '数据获取成功(备用)',
      data: {
        total_items: '0',
        favorite_items_count: 0,
        price_rising: 0,
        price_falling: 0,
        top_favorite_items: []
      }
    };
  }
}

/**
 * 获取最受欢迎/热门物品
 * @param limit 限制数量
 * @returns Promise
 */
export async function getMostFavoriteItems(limit: number = 10): Promise<ApiResponse<DashboardStats>> {
  return apiClient.get<DashboardStats>(API_ENDPOINTS.MOST_FAVORITE_ITEMS, { params: { limit } })
}

/**
 * 获取地图密码
 * @returns Promise
 */
export async function getMapPasswords(): Promise<ApiResponse<MapPassword>> {
  return apiClient.get<MapPassword>(API_ENDPOINTS.MAP_PASSWORDS)
}

/**
 * 获取价格变动物品列表
 * @returns Promise
 */
export async function getPriceChangeItemsList(): Promise<ApiResponse<{
  increase_items: Array<{
    id: number
    name: string
    image_url: string
    current_price: number
    previous_price: number
    increase_rate: number
    change_type: 'increase'
    grade?: number
  }>
  decrease_items: Array<{
    id: number
    name: string
    image_url: string
    current_price: number
    previous_price: number
    decrease_rate: number
    change_type: 'decrease'
    grade?: number
  }>
}>> {
  return apiClient.get<any>('/index/getPriceChangeItemsList')
}
